package com.yoo.ons.function.like

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.SimpleItemAnimator
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.gyf.immersionbar.ImmersionBar
import com.yoo.chat.kit.utils.StatusBarUtil
import com.yoo.module_base.utils.log
import com.yoo.ons.base.BaseVMFragment
import com.yoo.ons.base.adpater.isRefreshing
import com.yoo.ons.databinding.FragmentWlmBinding
import com.yoo.ons.dialog.ProfileInfoBannerDialogFragment
import com.yoo.ons.event.MessageEvent
import com.yoo.ons.function.like.adapter.LikeDataListAdapter
import com.yoo.ons.function.like.viewmodel.WhoLikeMeViewModel
import com.yoo.ons.model.WlmSubListModel
import org.greenrobot.eventbus.EventBus

const val WLM_ITEM_CLICK_UNLIKE = "WLM_ITEM_CLICK_UNLIKE"
const val WLM_ITEM_CLICK_LIKE = "WLM_ITEM_CLICK_LIKE"
const val WLM_ITEM_CLICK_MATCH = "WLM_ITEM_CLICK_MATCH"
const val WLM_ITEM_CLICK_EMPTY = "WLM_ITEM_CLICK_EMPTY"
const val WLM_ITEM_CLICK_ERROR = "WLM_ITEM_CLICK_ERROR"

class LikeListFragment : BaseVMFragment<FragmentWlmBinding, WhoLikeMeViewModel>(
    FragmentWlmBinding::inflate,
    WhoLikeMeViewModel::class.java
) {
    private val adapter by lazy { LikeDataListAdapter() }

    //保存点击查看信息弹窗的model
    private var modelBean: WlmSubListModel? = null

    @SuppressLint("SetTextI18n")
    override fun initData() {
        binding?.conWlmBox?.setPadding(
            0, StatusBarUtil.getStatusBarHeight(
                activity
            ), 0, 0
        )
        initialViews()
        dispatchRequest()
        mViewModel?.dataAllList?.observe(this, Observer {
            adapter.submitList(it)
            binding?.txtTwoFragmentDesc?.text =
                "${it.size-1} people have already liked you \uD83D\uDC97"
        })

        mViewModel?.loading?.observe(this, Observer {
            binding?.refreshLayout?.isRefreshing = it.isRefreshing()
        })

        mViewModel?.likeTypeLiveData?.observe(this) {
            dealLikeType(it)
        }
    }

    private fun dealLikeType(type: Int) {
        log("dealLikeType type=${type}")
        //1不喜欢就移除 2喜欢走匹配
        if (type == 1) {
            modelBean?.let { removeModel(it) }
        } else if (type == 2) {
            modelBean?.let { matchModel(it) }
        }
    }

    override fun onResume() {
        super.onResume()
        ImmersionBar.with(this).statusBarDarkFont(false).init()
    }


    override fun initListener() {

    }

    private fun dispatchRequest() {
        mViewModel?.reqIndexList(true)
    }

    override fun onDestroyView() {
        binding?.recyclerView?.adapter = null
        adapter.setListener(null)
        binding?.refreshLayout?.setOnRefreshListener(null)
        super.onDestroyView()
    }

    private fun initialViews() {
        binding?.refreshLayout?.setOnRefreshListener {
            dispatchRequest()
        }
        binding?.recyclerView?.layoutManager =
            StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        (binding?.recyclerView?.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
        //binding?.recyclerView?.layoutManager = GridLayoutManager(context, 2).apply { spanSizeLookup = PopularLayoutManagerSpanSizeLookUp(adapter) }
        //binding?.recyclerView?.itemAnimator = null
        binding?.recyclerView?.adapter = adapter
        adapter.setListener(::handleAdapterListener)
    }

    private fun handleAdapterListener(action: String, item: Any?) {
        when (action) {
            WLM_ITEM_CLICK_UNLIKE -> ((item as? View)?.tag as? WlmSubListModel)?.let(::removeModel)
            WLM_ITEM_CLICK_MATCH -> (item as? View)?.let(::showProfileInfo)
            WLM_ITEM_CLICK_LIKE -> ((item as? View)?.tag as? WlmSubListModel)?.let(::matchModel)
            WLM_ITEM_CLICK_EMPTY -> (item as? View)?.let(::goMain)
            WLM_ITEM_CLICK_ERROR -> (item as? View)?.let(::tryAgainCLick)
        }
    }

    private fun tryAgainCLick(itemView: View?) {
        itemView?.let {
            mViewModel?.reqIndexList(true)
        }
    }

    private fun goMain(itemView: View?) {
        itemView?.let {
            EventBus.getDefault().post(MessageEvent(2, ""))
        }
    }

    private fun matchModel(bean: WlmSubListModel) {
        removeModel(bean)
        MatchSuccessDialogFragment.newInstance(bean.avatarUrl, bean.userCodeFriend)
            .show(childFragmentManager)
    }

    private fun showProfileInfo(itemView: View?) {
        itemView?.let {
            modelBean = it.tag as WlmSubListModel
            ProfileInfoBannerDialogFragment.newInstance(modelBean?.userCodeFriend ?: "", 1)
                .show(activity?.supportFragmentManager)
            //ProfileInfoDialogFragment.newInstance(bean.userCodeFriend,1).show(childFragmentManager,"ProfileInfoDialogFragment")
        }
    }

    private fun removeModel(bean: WlmSubListModel) {
        mViewModel?.removeModel(bean.userCodeFriend)
    }

}

//private class PopularLayoutManagerSpanSizeLookUp(val adapter: LikeDataListAdapter) : GridLayoutManager.SpanSizeLookup() {
//    override fun getSpanSize(position: Int): Int {
//        return when (adapter.getItemViewType(position)) {
//            ItemType.LikeListData -> 1
//            else -> 2
//        }
//    }
//}